package com.dayaokeji.rhythmschoolstudent.client.common.resource;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;

import java.util.logging.Logger;

import butterknife.BindView;
import ir.sohreco.androidfilechooser.ExternalStorageNotAvailableException;
import ir.sohreco.androidfilechooser.FileChooser;


/**
 * 文件选择
 */
public class FileChooserActivity extends BaseActivity {

    public static final String EXTRA_CHOOSER_FILE_PATHS = "file_paths";
    private static final int STORE_PERMISSION_REQUEST_CODE = 10001;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_chooser);
        setSupportActionBar(toolbar);

        initFragment();
    }

    private void initFragment() {
        FileChooser.Builder builder = new FileChooser.Builder(FileChooser.ChooserType.FILE_CHOOSER, new FileChooser.ChooserListener() {
            @Override
            public void onSelect(String path) {
                String[] paths = path.split(FileChooser.FILE_NAMES_SEPARATOR);
//                Logger.d("upload file "+paths.toString());
                Intent intent = getIntent();
                intent.putExtra(EXTRA_CHOOSER_FILE_PATHS,paths);
                setResult(RESULT_OK,intent);
                ActivityStackManager.finish(FileChooserActivity.this);
            }
        });
        builder.setMultipleFileSelectionEnabled(false);  //设置是否多选
        builder.setSelectMultipleFilesButtonText("确定");
        builder.setSelectMultipleFilesButtonBackground(R.drawable.btn_common_bg_rect);
        try {
            getSupportFragmentManager().beginTransaction().replace(R.id.content,builder.build()).commit();
        } catch (ExternalStorageNotAvailableException e) {
            e.printStackTrace();
        }
    }
}
